% convenient_prices_REStat_figures.m
% Produces figures showing the convenience of prices.

clear all;
close all;

global data n_values fig_no location_categories locations product_categories products conv_codes n_col nbd_col

% For keeping track of figures
fig_no = 0;     
% Load dataset
load conv_prices.txt;
data = conv_prices;
% Data description
% Row 1: Dummy for whether convenience should matter (1) or not (0).
% Row 2: Classification code--categorizes dataset by location.
% Row 3: Whether exact change is required or not--0 for exact change does
% not matter, 1 for exact change/bills required, 2 for exact change only.
% Column 1: Classification code--categorizes dataset by product type.
% Column 2: Dummy for whether tax is not included in the posted price (1)
% or not (0--also, if the good is tax exempt, this takes on the value of
% zero).
% Enter the location/establishment-type data
location_categories = max(data(2,:));
locations = char('Post office','Coffee shops','Vending machines','Concession stands',...
    'Convenience stores','Market concession stands','Movie theatres','Coin laundries',...
    'Dry cleaners','Superstores','Apparel stores/bookstores','Public transportation',...
    'Dance clubs/bars','Taxi services','Parking lots','Toll roads','Barbershops',...
    'Fast food restaurants');
% Enter the product data
product_categories = max(data(:,1));
products = char('Movie tickets','Concession items (food, no drinks)','Coffee, tea, bagels',...
    'Drinks (Coca-Cola, Pepsi, bottled water)','Newspapers','Postage',...
    'University of Michigan apparel and souvenirs','Candy bars, gum, snacks','Other food products (fruits, vegetables, etc.)',...
    'Fares for public transportation','Coin laundry','Dry cleaning','Haircuts (men)','Club and bar products',...
    'Taxi rates','Flat taxi rates: Ann Arbor to Detroit Metro Airport','Parking rates','Toll roads and toll bridges','Cigarettes (single pack)',...
    'Fast food (single items)','Fast food (combos)');
% Load values for inconvenience parameters
load n_values.txt;
% Inconvenience parameter descriptions
% Column 1: Bilateral exchange, common coins and bills
% Column 2: Bilateral exchange, coins only (not used)
% Column 3: Unilateral exchange, common coins and bills
% Column 4: Unilateral exchange, coins only 
% Column 5: Bilateral exchange, use bills to make the purchase, potentially
% receive change
% Note: 
    % Common coins: penny, nickel, dime, quarter
    % Common bills: one, five, ten, twenty
% First, transform dataset to work with it.
prices = fn_sort_p(data);
% Drop fast food (single items) prices
ind1=(prices(:,4)~=20);
prices=prices(ind1,:);
conv_codes=conv_codes(ind1,:);
% Drop coin laundry prices
ind1=(prices(:,4)~=11);
prices=prices(ind1,:);
conv_codes=conv_codes(ind1,:);

% prices is a z x 5 vector, where z is the number of prices.  
% Column 1: The actual price.
% Column 2: The convenience dummy included in the dataset--i.e., the a
% priori convenience dummy.
% Column 3: The classification code by location.
% Column 4: The classification code by product.
% Column 5: The tax not included dummy.

% Exercise 1: Plot frequency distribution of all prices when convenience should (figure 1) and should not matter (figure 2)
freq1 = zeros(100,1);
total = length(prices);
numbs = 0;
text_size=14;
for i=1:total
    if prices(i,2) == 1     % Only want prices for which convenience should matter
        x = round(100*rem(prices(i,1),1));
        freq1(x+1,1) = freq1(x+1,1)+1;
        numbs = numbs + 1;
    end;
end;
freq_perc1 = freq1/numbs;
fig_no = fig_no + 1;
figure(fig_no)
set(plot([0:1:99],freq_perc1),'LineWidth',3,'Color','k');
set(gca,'FontName','Times','FontSize',text_size);
axis([-1 100 0 0.35]);
set(gca,'XTick',[0:5:95 99]);
set(gca,'XTickLabel','0| |10| |20| |30| |40| |50| |60| |70| |80| |90| |99');
set(gca,'YTick',0:0.05:0.35);
set(gca,'YTickLabel','0| 5%|10%|15%|20%|25%|30%|35%');
xlabel('Price ending, in cents');
ylabel('Percentage of prices');
% Redo for when convenience should not/does not matter
freq2 = zeros(100,1);
total = length(prices);
numbs = 0;
for i=1:total
    if prices(i,2) == 0     % Only want prices for which convenience doesn't matter
        x = round(100*rem(prices(i,1),1));
        freq2(x+1,1) = freq2(x+1,1)+1;
        numbs = numbs + 1;
    end;
end;
freq_perc2 = freq2/numbs;
fig_no = fig_no + 1;
figure(fig_no)
set(plot([0:1:99],freq_perc2),'LineWidth',3,'Color','k');
set(gca,'FontName','Times','FontSize',text_size);
axis([-1 100 0 0.35]);
set(gca,'XTick',[0:5:95 99]);
set(gca,'XTickLabel','0| |10| |20| |30| |40| |50| |60| |70| |80| |90| |99');
set(gca,'YTick',0:0.05:0.35);
set(gca,'YTickLabel','0| 5%|10%|15%|20%|25%|30%|35%');
xlabel('Price ending, in cents');
ylabel('Percentage of prices');

% Exercise 2: Frequency by currency multiple, by location
bar_data=fn_freq_location(prices);
bar_data_to_excel=zeros(size(bar_data));
v1=[3;4;5;2;6;7;13;9;14;15;12;17;16;1;18];
bar_data_to_excel(1:15,:)=bar_data(v1,:);
bar_data_to_excel(17:18,:)=bar_data([11;10],:);
for i1=1:size(bar_data_to_excel,1);
	bar_data_to_excel(i1,5)=sum(bar_data_to_excel(i1,5:8));
end;
bar_data_to_excel=bar_data_to_excel(:,1:5); % export for plotting
bar_data_to_excel_labels=char('Vending machines','Concession stands','Convenience stores','Coffee shops','Market concession stands',...
	'Movie theatres','Dance clubs/bars','Dry cleaners','Taxi services','Parking lots','Public transportation','Barbershops',...
	'Toll roads','Post office','Fast food restaurants',' ','Apparel stores/bookstores','Superstores');
bar_data_to_excel_key=char('Require pennies','Multiples of a nickel','Multiples of a dime','Multiples of a quarter','Multiples of bills ($1, $5, etc.)');